home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5911 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.5 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Initialising structure members - help please!
  5. Date: 20 Feb 1996 16:45:14 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb20094514@qcd.lanl.gov>
  8. References: <4gb8hn$3m8@news.mistral.co.uk> <31298EF1.5F2C@cmt.lpr.mail.carel.fi>
  9.     <4gckb1$77e@news.mistral.co.uk>
  10. NNTP-Posting-Host: qcd.lanl.gov
  11. Mime-Version: 1.0
  12. Content-Type: text
  13. In-reply-to: mikebarnard@mistral.co.uk's message of Tue, 20 Feb 1996 11:54:07 GMT
  14.  
  15. In article <4gckb1$77e@news.mistral.co.uk> mikebarnard@mistral.co.uk
  16. (Mike Barnard) writes: 
  17. <snip>
  18.    >You can't copy C strings with an "=".  You must use the strcpy() function.
  19.  
  20.    and...
  21.  
  22.    >Since you declared 'description' as an array of chars, use strcpy(one.description, 
  23.    >"whatever string"); Be sure you won't use longer strings than the space available. A 
  24.    >better approach would be to declare 'decription' as a 'char *' and then malloc 
  25.  
  26. Yet better is to declare it const char * then. String literals are
  27. arrays of type `char []', which in most uses decay to `char*' and can
  28. be assigned to `const char*'. However, like `const char[]' objects,
  29. string literals cannot be modified portably. Unlike `const char[]' objects,
  30. attempts to modify them do not, by themselves, require a diagnostic
  31. from the compiler.
  32.  
  33. Assigning them to `const char*' guarantees that unless explicit casts
  34. (or other tricks involving library functions) are used, any attempt to
  35. modify them will generate a diagnostic.
  36.  
  37.    >enough space for whatever string you're going to place into it. (Also, your syntax 
  38.    >would work in that case.) The last two will work if you add & in front of them.
  39.  
  40.    Thanks, I'll look at what this means. But as a learning question, (not
  41.    an argumentative one!!!),  why does this good old boy work then?
  42.  
  43.    char name[]="An array of characters";
  44.  
  45.    Surely "name" is an array of characters as is my "description"? It
  46.    works here. Doesn't it? This leads me on to another question actually,
  47.    but I'll start another thread with it I think. 
  48.  
  49. Normally, in C, arrays cannot be assigned from other arrays. The
  50. reason for this is historical: arrays are not really first class
  51. citizens of the C type system. Whenever one uses an array from memory
  52. in an expression, _except_ as an immediate argument of & (address of)
  53. or sizeof operators, it is converted to a pointer to its first element
  54. instead. This conversion is usually referred to as the `decay of the
  55. array'. In other words, in almost all expressions, an array a can be
  56. replaced by the expression (&a[0]).
  57.  
  58. Now, here is the problem. A variable declared as an array is an
  59. lvalue: i.e. it refers to a region of storage. However, (&a[0]) is an
  60. expression which designates a pointer: it does not designate any
  61. region of memory. What would it mean to change it? As (&a[0]) =
  62. (&b[0]) does not make sense, neither does a = b! Note that even if we
  63. had changed the rules of C to say that lhs of = does not decay, we
  64. still would have a problem of trying to assign a pointer to an
  65. array. We could have had a rule which says that neither side of the =
  66. decays, but that would involve copying entire arrays; and in the early
  67. days of C, people thought that compilers should not be so `smart' and
  68. do complicated things `behind programmer's back'. Now, it is too late
  69. to change this.
  70.  
  71. Now, declarations are different. An array being declared is not
  72. sitting inside an expression (naturally). The rule for decays applies
  73. only for arrays in expressions. Remembering that the initializer (the
  74. stuff that initializes a declared object: a part of the declaration,
  75. not a separate statement), is in an expression context; the language
  76. still does not allow `int a[] = b' where b is an array (how can one
  77. initialize an array with a pointer to which b decays?). In fact, for
  78. non-simple objects (i.e. things like arrays and structs), the compiler
  79. says that whatever you use to initialize it must be a bunch of
  80. constants. So, typically, one writes `int a[] = {1,2,3}' to initialize
  81. an array.
  82.  
  83. At this point, one notices that one kind of initialization is very
  84. common: a character array. This could always be written in the usual
  85. form: `char a[] = {'h', 'e', 'l', 'l', 'o', '\0'}', but people decided
  86. that `char a[] = "hello"' would be a useful shorthand for this. Note
  87. that string literals (and wide string literals) are special: they are
  88. the only constants which are arrays, and a little leeway for them
  89. doesn't hurt :-)
  90.  
  91. Note that there is another peculiarity. "hello" normally expands to
  92. {'h', 'e', 'l', 'l', 'o', '\0'} as mentioned above, but when
  93. initializing an array whose length is specified as 5, it is equivalent
  94. to {'h', 'e', 'l', 'l', 'o'} This special treatment of the ending '\0'
  95. is also part of the magic of string literal initializers.
  96.  
  97. And by the way, though the string literal arrays are constant, they
  98. can happily initialize non-constant arrays. In fact, in many respects,
  99. it is best to think of them not as literals at all in this context,
  100. but as an abbreviation mentioned in the last paragraph.
  101.  
  102. Do things clear up somewhat now?
  103.  
  104. Cheers
  105. Tanmoy
  106. --
  107. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  108. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  109. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  110. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  111. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  112. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  113.